package org.opens.mybatisplus.controller;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping(value = "/api")
public class ExcelController {
//TODO 下面是poi3.x版本的导入方式, 升级了poi为4.x, 需要升级这里
/*    @RequestMapping(value = "/excel/import", method = RequestMethod.POST)
    public R importExcel(
            @RequestParam(value = "excelFile") MultipartFile excelFile,
            @RequestParam(value = "industryId", required = true) Integer industryId
    ) {
        int importSuccessRowNumber = 0;
        int totalRowNumber = 0;
        Workbook workbook = null;

        try {
            workbook = ExcelUtil.getWorkBook(excelFile.getInputStream(), excelFile.getOriginalFilename());
        } catch (IOException e) {
            e.printStackTrace();
            return R.error("不支持的文件类型");
        }
        Sheet sheet = workbook.getSheetAt(0);
        totalRowNumber = sheet.getLastRowNum();

        List<String> typeName2 = new ArrayList<>();
        for (int i = 1; i <= sheet.getLastRowNum(); i++) {
            Row row = sheet.getRow(i);
            if(row.getCell(0).getCellType() != Cell.CELL_TYPE_STRING) {
                continue;
            }
            String typeNameTemp = row.getCell(0).getStringCellValue();
            if(StrUtil.isBlank(typeNameTemp)) {
                continue;
            }
            if(typeName2.contains(typeNameTemp)) {
                continue;
            }
            typeName2.add(typeNameTemp);
            if(row.getCell(1).getCellType() != Cell.CELL_TYPE_STRING) {
                continue;
            }
            String weightsTemp = row.getCell(1).getStringCellValue();
            if(StrUtil.isBlank(weightsTemp)) {
                continue;
            }
            importSuccessRowNumber++;
        }
        R success = R.success("成功");
        success.put("industryId", industryId);
        success.put("importSuccessRowNumber", importSuccessRowNumber);
        success.put("totalRowNumber", totalRowNumber);
        return success;
    }

    @RequestMapping(value = "/excel/import/verify", method = RequestMethod.POST)
    public R verifyOfExcel(
            @RequestParam(value = "excelFile") MultipartFile excelFile,
            @RequestParam(value = "industryId", required = true) Integer industryId
    ) {
        String fileName = excelFile.getOriginalFilename();
        ExcelUtil.ExcelType excelType = ExcelUtil.getExcelType(fileName);
        System.out.println("-->" + excelType);
        if(excelType == ExcelUtil.ExcelType.ERROR_FILE) {
            return R.error("不支持的文件类型");
        }
        Workbook workbook = null;
        try {
            workbook = ExcelUtil.getWorkBook(excelFile.getInputStream(), fileName);
        } catch (IOException e) {
            e.printStackTrace();
            return R.error("不支持的文件类型");
        }
        if(ExcelUtil.isEmpty(workbook)) {
            return R.warning("该excel为空");
        }
        R success = R.success("验证通过");
        success.put("industryId", industryId);
        return success;
    }*/

}
